<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="展会名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" size="mini" @click="handleedit('add')"
          >新增</el-button
        >
        <el-button type="danger" size="mini" @click="handleDelete('all')"
          >删除</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      ref="multipleTable"
      v-loading="loading"
      :data="supplierList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55"> </el-table-column>
      <el-table-column label="序号" align="center" prop="id" width="100">
        <template slot-scope="scope">
          <div>
            {{
              scope.$index +
              (queryParams.pageNum - 1) * queryParams.pageSize +
              1
            }}
          </div>
        </template>
      </el-table-column>
      <el-table-column
        label="展会名称"
        align="center"
        prop="name"
        show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column label="可见权限" align="center" prop="title">
        <template slot-scope="scope">
          <div>
            {{
              scope.row.title == "1"
                ? "中国"
                : scope.row.title == "2"
                ? "非洲"
                : "所有"
            }}
          </div>
        </template>
      </el-table-column>
      <el-table-column
        label="展会图片"
        align="center"
        prop="img"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          <div v-if="Array.isArray(scope.row.img)">
            <el-image
              style="width: 30px; height: 30px"
              :src="scope.row.img[0].url"
              :preview-src-list="[scope.row.img[0].url]"
            >
            </el-image>
          </div>
          <div v-else>
            <el-image
              style="width: 30px; height: 30px"
              :src="scope.row.img"
              :preview-src-list="[scope.row.img]"
            >
            </el-image>
          </div>
        </template>
      </el-table-column>
      <!-- <el-table-column
        label="展会地点"
        align="center"
        prop="exhibitionaddress"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        label="展会时间"
        align="center"
        prop="exhibitiontime"
        :show-overflow-tooltip="true"
      >
      </el-table-column> -->
      <el-table-column
        label="浏览量"
        align="center"
        prop="views"
        :show-overflow-tooltip="true"
        v-if="routername == 'chinaafic'"
      >
      </el-table-column>
      <el-table-column
        label="视频时长"
        align="center"
        prop="video"
        :show-overflow-tooltip="true"
        v-if="routername == 'videolist'"
      >
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status" width="100">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status == '1'"
            active-color="#13ce66"
            inactive-color="#ff4949"
            @change="changestate(scope.row)"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column
        label="更新时间"
        align="center"
        prop="updateTime"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        prop="opration"
        width="200"
        fixed="right"
      >
        <template slot-scope="scope">
          <!-- <el-button
            type="text"
            size="mini"
            icon="el-icon-edit"
            @click="handleedit('edit', scope.row)"
            >修改</el-button
          > -->
          <el-button
            type="text"
            size="mini"
            @click="handleedit('more', scope.row)"
            >详情</el-button
          >
          <el-button
            type="text"
            size="mini"
            icon="el-icon-delete"
            style="color: red"
            @click="handleDelete('del', scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改公告对话框 -->
    <el-dialog
      :title="title"
      :visible="open"
      width="780px"
      append-to-body
      :before-close="handeclose"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="展会名称:" prop="name">
              <el-input
                v-model="form.name"
                placeholder="请输入"
                style="width: 90%"
                :disabled="isdisable"
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="展会介绍" prop="expoIntroduce">
              <editor
                style="width: 90%"
                v-model="form.expoIntroduce"
                :min-height="102"
                v-if="!isdisable"
              />
              <div style="content-box" v-else>
                <div
                  style="width: 90%; line-height: 15px"
                  v-html="form.expoIntroduce"
                ></div>
              </div>
            </el-form-item>
            <el-form-item label="封面图:" prop="img" class="formdata">
              <ImageUpload
                :limit="1"
                :fileSize="fileSize"
                :isShowTip="false"
                uploadtype="image"
                :fileList.sync="form.img"
                ref="tableupload"
                v-if="!isdisable"
              />
              <div v-else>
                <el-image
                  style="width: 50px; height: 50px"
                  :src="form.img[0].url || form.img"
                  :preview-src-list="[form.img[0].url || form.img]"
                >
                </el-image>
              </div>
            </el-form-item>
            <el-form-item label="可见权限:" prop="isShow">
              <el-radio-group v-model="form.isShow" :disabled="isdisable">
                <el-radio
                  v-for="(item, index) in jurisdictionoption"
                  :key="index"
                  :label="item.value"
                  >{{ item.label }}</el-radio
                >
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { expolist, addexpo, editexpo, delexpo } from "@/api/website/investment";
import Editor from "@/components/Editor";
import selfDirective from "@/utils/selfDirective";
import FileUpload from "@/components/FileUpload/index";
import ImageUpload from "@/components/ImageUpload/index";
import { Base64 } from "js-base64";

export default {
  name: "supplier",
  components: {
    Editor,
    FileUpload,
    ImageUpload,
  },
  data() {
    var checkPhone = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("手机号不能为空"));
      } else {
        const reg = /^1[3|4|5|7|8][0-9]\d{8}$/;
        if (reg.test(value)) {
          callback();
        } else {
          return callback(new Error("请输入正确的手机号"));
        }
      }
    };
    return {
      // 遮罩层
      loading: false,
      routername: "",
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      fileSize: 0.5,
      // 公告表格数据
      supplierList: [
        {
          title: "TX2021001010",
          businessName: "天路电商",
          withdrawal: 10,
          ServiceCharge: 10,
          arrival: 10,
          updateTime: "2020-01-02",
          bankaccount: "6222021901024548990",
          bankname: "中国工商银行",
          accountname: "湖南星图广告有限公司",
          IBAN: "无",
          SwiftCode: "ICBKCNBJHUN",
          branchCode: "无",
          bankaddress: "长沙市芙蓉区五一大道100号",
        },
      ],
      // 弹出层展会名称
      title: "",
      // 是否显示弹出层
      open: false,
      // 状态数据字典
      typeOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        withdrawal: "0",
      },
      // 表单参数
      form: {
        title: "",
        Productpicture: [],
      },
      // 表单校验
      rules: {
        name: [
          {
            required: true,
            message: "展会名称不能为空",
            trigger: "change",
          },
        ],
        img: [
          {
            required: true,
            message: "封面图不能为空",
            trigger: "change",
          },
        ],
        expoIntroduce: [
          {
            required: true,
            message: "展会介绍不能为空",
            trigger: "change",
          },
        ],
        isShow: [
          {
            required: true,
            message: "请选择可见权限",
            trigger: "change",
          },
        ],
      },
      issupplier: true,
      limit: 5,
      fileSize: 20,
      isShowTip: false,
      value: [],
      nationoptions: [],
      categoryoptions: [],
      companyoptions: [],
      gysTypeOptions: [],
      payEemerchantNooptions: [],
      isdisable: false,
      isdisabletwo: false,
      msgvisible: false,
      //银行信息
      bankmsg: {
        bankname: "",
        accountname: "",
        bankaccount: "",
        IBAN: "",
        SwiftCode: "",
        branchCode: "",
        bankaddress: "",
      },
      statusoptions: [
        {
          dictValue: "1",
          dictLabel: "已启用",
        },
        {
          dictValue: "2",
          dictLabel: "已禁用",
        },
      ],
      jurisdictionoption: [
        {
          label: "所有",
          value: "0",
        },
        {
          label: "中国",
          value: "1",
        },
        {
          label: "非洲",
          value: "2",
        },
      ],
    };
  },
  created() {
    this.getList();
    this.getDicts("gys_type").then((response) => {
      this.categoryoptions = response.data;
    });
    this.getDicts("gys-gj").then((response) => {
      this.nationoptions = response.data;
    });
    this.getDicts("pay-eemer-chantNo").then((response) => {
      this.payEemerchantNooptions = response.data;
    });
  },
  methods: {
    handleSelectionChange(val) {
      this.selection = val;
    },
    getbankaccount(row) {
      let bankaccount = "";
      if (row.bankaccount) {
        bankaccount =
          "*******" +
          row.bankaccount.slice(
            row.bankaccount.length - 4,
            row.bankaccount.length
          );
      } else {
        bankaccount = "";
      }
      return bankaccount;
    },
    /** 查询公告列表 */
    handelcopy() {
      copysupplier().then((res) => {
        this.msgSuccess("复制成功");
      });
    },
    /** 查询公告列表 */
    changeStatus(value) {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 查询公告列表 */
    handlecord(row) {
      this.msgvisible = true;
      this.bankmsg = row;
    },
    /** 查询公告列表 */
    getList() {
      this.loading = true;
      expolist(this.queryParams).then((response) => {
        var supplierList = response.rows || [];
        supplierList.map((item) => {
          if (item.img) {
            item.img = this.isJSON(item.img) ? JSON.parse(item.img) : item.img;
          }
          item.isShow = item.isShow
            ? item.isShow.toString()
            : item.isShow == 0
            ? "0"
            : item.isShow;
          item.status = item.status
            ? item.status.toString()
            : item.status == 0
            ? "0"
            : item.status;
        });
        this.supplierList = supplierList;
        this.total = response.total;
        this.loading = false;
      });
    },
    isJSON(str) {
      if (typeof str == "string") {
        try {
          JSON.parse(str);
          return true;
        } catch (e) {
          return false;
        }
      }
    },
    // 公告状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 公告状态字典翻译
    typeFormat(row, column) {
      return this.selectDictLabel(this.typeOptions, row.noticeType);
    },
    // 取消按钮
    cancel() {
      if (this.title == "详情") {
        this.open = false;
      } else {
        this.open = false;
        this.reset();
      }
    },
    // 表单重置
    reset() {
      this.resetForm("form");
      this.form = {
        id: undefined,
        img: [],
        isShow: "0",
      };
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 提交按钮 */
    submitForm: function () {
      if (this.title == "详情") {
        this.open = false;
        return;
      }
      this.$refs["form"].validate((valid) => {
        if (valid) {
          let expoIntroduce = Base64.encode(this.form.expoIntroduce);
          let img = JSON.stringify(this.form.img);
          if (this.form.id != undefined) {
            editexpo(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            let obj = {
              ...this.form,
              img: img,
              expoIntroduce: expoIntroduce,
            };
            addexpo(obj).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    handleedit(type, row) {
      this.open = true;
      if (type == "edit") {
        this.reset();
        this.form = row;
        this.title = "修改";
        this.isdisable = false;
        this.isdisabletwo = true;
      } else if (type == "add") {
        this.reset();
        this.title = "新增";
        this.isdisable = false;
      } else if (type == "more") {
        this.title = "详情";
        this.form = row;
        this.isdisable = true;
        this.isdisabletwo = true;
      }
    },
    handeclose() {
      this.open = false;
    },
    getdictlabel(row, nationoptions, name) {
      let str = "";
      nationoptions.map((item) => {
        if (item.dictValue == row[name]) {
          str = item.dictLabel;
        }
      });
      return str;
    },
    /** 删除按钮操作 */
    handleDelete(type, row) {
      let ids;
      if (type == "all") {
        let selection = this.selection;
        if (selection.length == 0) {
          this.$message.error("请选择一条删除数据");
          return;
        }
        let idsarr = [];
        selection.map((item) => {
          idsarr.push(item.id);
        });
        ids = idsarr.join(",");
      } else {
        ids = row.id;
      }
      var that = this;
      this.$confirm("是否确认删除该仓库?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          that.selection = [];
          return delexpo(ids);
        })
        .then(() => {
          this.getList();
          this.$message.success("删除成功");
        })
        .catch(() => {});
    },
    changestate(row) {
      var that = this;
      const statusstr = row.status == 1 ? "禁用" : "启用";
      this.$confirm("是否确认" + statusstr + "该供应商?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(function () {
        var obj = {
          id: row.id,
          status: row.status == 1 ? 0 : 1,
        };
        editexpo(obj).then((response) => {
          that.$message.success(statusstr + "成功");
          that.getList();
        });
      });
    },
  },
};
</script>
<style scoped lang="scss">
.radio-box {
  margin-bottom: 10px;
}
.stock {
  text-decoration: underline;
}
.copy {
  color: #999;
  cursor: pointer;
}
.box-card {
  margin-top: 20px;
  .card-title {
    font-weight: 700;
    font-style: normal;
    color: #555555;
  }
  .baseinfo {
    width: 100%;
    height: 40px;
    line-height: 40px;
    color: #999;
    background-color: #f2f2f2;
    padding-left: 10px;
    border-radius: 5px;
    margin-top: 10px;
  }
}
.dialog-footer {
  display: flex;
  justify-content: center;
  align-items: center;
}
.lookcard {
  text-decoration: underline;
  color: #1890ff;
  cursor: pointer;
}
.banktable {
  td {
    padding: 5px;
  }
}

::v-deep {
  .el-table .cell {
    white-space: pre-line !important; /*保留换行符*/
  }
}
.formdata ::v-deep {
  .el-form-item__label {
    white-space: pre-line !important; /*保留换行符*/
    line-height: 15px;
  }
}
.content-box {
  border: 1px solid #999;
  padding: 5px;
  height: 200px;
  overflow-y: auto;
  line-height: 15px;
}
</style>